package com.tjsiqi.service;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.GybWeighRecordMapper;
import com.tjsiqi.dto.WeighCountDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.GybWeighRecord;
import com.tjsiqi.model.GybWeighRecordEXP;
import com.tjsiqi.param.WeighCountParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysWeighOrderService {
    @Autowired
    private GybWeighRecordMapper gybWeighRecordMapper;

    //  查询进货单
    public PageResult<GybWeighRecord> searchPageList(WeighCountParam WeighCountParam, PageQuery page) {

        WeighCountDto WeighCountDto = getWeighCDto(WeighCountParam);

        int count = gybWeighRecordMapper.getWeighOrder(WeighCountDto);

        System.out.println("WeighJblWeighTruckOUTVoCount"+count);
        if (count > 0){
            List<GybWeighRecord> WeighRecordVoList = gybWeighRecordMapper.getPageList(WeighCountDto, page);
            for(GybWeighRecord gybWeighRecord:WeighRecordVoList){
                System.out.println("WeighJblWeighTruckOUTVo"+gybWeighRecord);
            }
            return PageResult.<GybWeighRecord>builder().total(count).data(WeighRecordVoList).build();
        }
        return PageResult.<GybWeighRecord>builder().build();
    }

    private static WeighCountDto getWeighCDto(WeighCountParam weighCountParam){
        WeighCountDto weighCountDto = new WeighCountDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(weighCountParam.getFromTime())) {
                weighCountDto.setFromTime(dateFormat.parse(weighCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                weighCountDto.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(weighCountParam.getToTime())) {
                weighCountDto.setToTime(dateFormat.parse(weighCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                weighCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题,正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        weighCountDto.setJbzCode(weighCountParam.getJbzCode());

        return weighCountDto;
    }

//    public List<GybWeighRecordEXP> exportByParam(WeighCountParam weighCountParam) {
//    }

    //  导出进货单
    public List<GybWeighRecordEXP> exportByParam(WeighCountParam WeighCountParam) {

        WeighCountDto WeighCountDto = getWeighCDto(WeighCountParam);

        List<GybWeighRecordEXP> WeighOrderList = gybWeighRecordMapper.exportByParam(WeighCountDto);

        return WeighOrderList;
    }
    //  删除所选搅拌站
    public int removeByParam(WeighCountParam weighCountParam) {

        int isRemove = gybWeighRecordMapper.removeByParam(weighCountParam);
        return isRemove;
    }
}
